﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.ViewModels;
using System.ComponentModel;
using System.Windows.Media;
using WCYCReader.Models;
using WCYCReader.Utility;
using System.Threading;
using WCYCReader.Main.Core;
using WCYCReader.Main;

namespace WCYCReader.Pages
{
    public partial class BookChapterPage : PhoneApplicationPage
    {
        private ChapterInfoList_VM chapterList = new ChapterInfoList_VM();

        private MarkerInfoList_VM markerList = new MarkerInfoList_VM();

        protected PerformanceProgressBar chapterProgressBar = new PerformanceProgressBar();

        

        public BookChapterPage()
        {
            this.InitializeComponent();
            base.BackKeyPress += new EventHandler<CancelEventArgs>(this.OnBackKeyPress);
        }

        protected void AddPerformanceProgressBar()
        {
            if (!this.chapterProgressBar.IsIndeterminate)
            {
                this.chapterProgressBar.IsIndeterminate = true;
                this.chapterProgressBar.Height = 20;
                this.chapterProgressBar.Foreground = new SolidColorBrush(Colors.Red);
            }
        }

        private void chapterListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedIndex >= 0)
            {
                if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
                {
                    LocalBookProgressList_M valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressList_M;
                    valueFromSettings.int_ReaderIndex_M = this.chapterList.T_ChapterInfoList_VM()[listBox.SelectedIndex].ChapterList_Index;
                    SettingHelper.SaveInToSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook"), valueFromSettings);
                }
                base.NavigationService.GoBack();
            }
            listBox.SelectedIndex = -1;
        }

        private void ContentGrid_Loaded(object sender, RoutedEventArgs e)
        {
            App.QQReader_prePageId = App.QQReader_pageId;
            App.QQReader_pageId = "QQReader_ChapterPage";
            this.LayoutRoot.Children.Add(this.chapterProgressBar);
            this.AddPerformanceProgressBar();
            (new Thread(() => Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (!App.QQReader_StorageFile.IsFile(string.Concat(App.ReaderFileName, "chapterList")))
                {
                    if (!(new ChapterParser(App.ReaderFileName)).Parser())
                    {
                        Prompt.PromptMessage("提示：智能断章错误！");
                    }
                    else
                    {
                        Prompt.PromptMessage("提示：智能断章成功！");
                    }
                }
                this.chapterListBox.ItemsSource = this.chapterList.T_ChapterInfoList_VM();
                this.chapterProgressBar.IsIndeterminate = false;
            }))).Start();
            this.markerListBox.ItemsSource = this.markerList.T_MarkerInfoList_VM();
        }

        private void ContentGrid_Unloaded(object sender, RoutedEventArgs e)
        {
        }

     

        private void markerListBox_Click(object sender, RoutedEventArgs e)
        {
            BookMarkerList_M bookMarkerListM = new BookMarkerList_M();
            bookMarkerListM = (sender as MenuItem).DataContext as BookMarkerList_M;
            if (bookMarkerListM != null)
            {
                if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
                {
                    LocalBookProgressList_M valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressList_M;
                    valueFromSettings.int_ReaderIndex_M = bookMarkerListM.MarkerList_Index;
                    SettingHelper.SaveInToSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook"), valueFromSettings);
                }
                base.NavigationService.GoBack();
            }
        }

        private void markerListBox_Delete(object sender, RoutedEventArgs e)
        {
            BookMarkerList_M bookMarkerListM = new BookMarkerList_M();
            bookMarkerListM = (sender as MenuItem).DataContext as BookMarkerList_M;
            if (bookMarkerListM != null)
            {
                this.markerList.Delete_MarkerInfoList(bookMarkerListM);
            }
        }

        private void markerListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedIndex >= 0)
            {
                if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
                {
                    LocalBookProgressList_M valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressList_M;
                    valueFromSettings.int_ReaderIndex_M = this.markerList.T_MarkerInfoList_VM()[listBox.SelectedIndex].MarkerList_Index;
                    SettingHelper.SaveInToSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook"), valueFromSettings);
                }
                base.NavigationService.GoBack();
            }
            listBox.SelectedIndex = -1;
        }

        public void OnBackKeyPress(object sender, CancelEventArgs e)
        {
            base.NavigationService.GoBack();
        }
    }
}